<?php

namespace App\Http\Controllers\Admin;

use App\Http\Requests\StoreStore;
use App\Http\Requests\UpdateStore;
use App\Models\BespeakCate;
use App\Models\Company;
use App\Models\Fixfxs;
use App\Models\Oncustomer;
use App\Models\Onorder;
use App\Models\Pack;
use App\Models\Store;
use App\Models\Config;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;

class StoreController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function __construct()
    {
        view()->share([
            '_stores' => 'am-in',
            '_store' => 'am-active',
        ]);

    }

     //门店首页
    public function index()
    {
        $stores = Store::paginate(10);
        return view('admin.store.index',compact('stores'));
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    //新增页面
    public function create()
    {
        return view('admin.store.create');
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request $request
     * @return \Illuminate\Http\Response
     */
    //执行新增
    public function store(StoreStore $request)
    {
       Store::create($request->all());
       return redirect('/admin/store')->with('success','新增门店成功');
    }

    /**
     * Display the specified resource.
     *
     * @param  int $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int $id
     * @return \Illuminate\Http\Response
     */
    //编辑页面
    public function edit($id)
    {
        //当前门店
        $store=Config::find($id);
        return view('admin.store.edit',compact('store'));
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request $request
     * @param  int $id
     * @return \Illuminate\Http\Response
     */
    //执行更新
    public function update(UpdateStore $request, $id)
    {
        //当前门店
        $store=Co::find($id);
        $data['name']=$request->name;
        $info=Store::where($data)->first();
       //保证门店名唯一性
     if($store['name'] != $request->name && $info){
         return back()->with('success','当前名称以有门店使用');
     }else{
         $store->update($request->all());
         return redirect('/admin/store')->with('success','修改成功');
     }

    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int $id
     * @return \Illuminate\Http\Response
     */
    //待完善，一般不可删除，如需删除需判断当前门店是否有会员，公司，私教，挑战等等。
    public function destroy($id)
    {


        $pack=Pack::where('store_id',$id)->first();
        if($pack){
            return back()->with('success','当面门店有挑战活动不可删除，请先删除挑战');
        }
        $fixfxs=Fixfxs::where('store_id',$id)->first();
        if($fixfxs){
            return back()->with('success','当面门店有教练不可删除,请先删除教练');
        }
        $company=Company::where('store_id',$id)->first();
        if($company){
            return back()->with('success','当面门店有公司不可删除，请先删除公司');
        }else{
            Store::destroy($id);
            return back()->with('success','删除成功');
        }

    }
}
